#include "mainwindow.h"

#include <Qt/qfiledialog.h>

namespace plok
{

    MainWindow::MainWindow()
    {
        ui.setupUi(this);

        connect(&m_player, SIGNAL(updateTime(qint64)), this, SLOT(on_updateTime(qint64)));
    }

    MainWindow::~MainWindow()
    {

    }

    void MainWindow::loadPlaylist (void)
    {       
        QStringList playlist_names;
        for (unsigned int i=0; i < m_playlist.size(); i++)
            playlist_names.append( QString::fromStdString (
                    m_playlist.at(i)->get_artist_from_tag() + " - " +
                    m_playlist.at(i)->get_album_from_tag() + " - " +
                    m_playlist.at(i)->get_title_from_tag()));

        ui.playlistWidget->clear();
        ui.playlistWidget->addItems (playlist_names);

//        if (m_playlist.size())
//        {
//            ui.playlistWidget->setCurrentRow(0);
//
//            for (int i=0; i < m_playlist.size(); i++)
//                m_player.enqueue( m_playlist.at( i ).filePath().toStdString() );
//        }
    }

    void MainWindow::on_actionOpen_file_triggered (void)
    {
        QString file_name = QFileDialog::getOpenFileName ();

        QSharedPointer<Audio_file_phonon> audio_file (new Audio_file_phonon (file_name.toStdString()));

        connect(audio_file.data(), SIGNAL(updateInfo(void)), this, SLOT(loadPlaylist(void)));

        m_playlist.push_back( audio_file );

        loadPlaylist ();
    }

    void MainWindow::on_actionOpen_dir_triggered (void)
    {
        QString dir_name = QFileDialog::getExistingDirectory(
            this,
            "Open directory",
            "/home",
            QFileDialog::ShowDirsOnly);

        QDir dir (dir_name, "*.flac");
        QFileInfoList file_list = dir.entryInfoList();

        for (int i=0; i<file_list.size(); i++)
        {
            QString path = file_list.at(i).filePath();

            QSharedPointer<Audio_file_phonon> audio_file (new Audio_file_phonon (path.toStdString()));

            connect(audio_file.data(), SIGNAL(updateInfo(void)), this, SLOT(loadPlaylist(void)));

            m_playlist.push_back( audio_file );
        }

        loadPlaylist ();
    }

    void MainWindow::on_playButton_clicked (void)
    {
        // Can't play a empty list
        if (!ui.playlistWidget->count())
            return;

        int current_index = ui.playlistWidget->currentRow();

        if (current_index == -1)
            current_index = 0;

        m_player.open( m_playlist.at( current_index )->get_file_location() );
        m_player.play();
    }

    void MainWindow::on_stopButton_clicked (void)
    {
        m_player.stop();
    }

    void MainWindow::on_playlistWidget_itemActivated (QListWidgetItem* item)
    {
        int current_index = ui.playlistWidget->currentRow();

        m_player.open( m_playlist.at( current_index )->get_file_location() );
        m_player.play();
    }

    void MainWindow::on_timeSlider_valueChanged (int value)
    {
        qint64 new_time = (m_player.totalTime() * value) / 100;

        m_player.seek (new_time);
    }

    void MainWindow::on_updateTime (qint64 time)
    {
        qint64 total_time = m_player.totalTime();

        int slider_value = (time * 100) / total_time;

        ui.timeSlider->blockSignals (true);
        ui.timeSlider->setValue(slider_value);
        ui.timeSlider->blockSignals (false);

        QString current_time_string = QString::number(time / 3600000) + ":" +
                                      QString::number((time / 60000) % 60) + ":" +
                                      QString::number((time / 1000) % 60);

        QString total_time_string = QString::number(total_time / 3600000) + ":" +
                                    QString::number((total_time / 60000) % 60) + ":" +
                                    QString::number((total_time / 1000) % 60);

        ui.timeValue->setText(current_time_string + " / " + total_time_string);
    }

}
